{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "ImportError",
     "evalue": "No module named basemap",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mImportError\u001b[0m                               Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-1-a0ffc4ac0b65>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;31m# Feel free to use or modify this script.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0mmpl_toolkits\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mbasemap\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mBasemap\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      5\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mmatplotlib\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpyplot\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mplt\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      6\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mnumpy\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mImportError\u001b[0m: No module named basemap"
     ],
     "output_type": "error"
    }
   ],
   "source": [
    "# Written by Vamei, http://www.cnblogs.com/vamei/\n",
    "# Feel free to use or modify this script.\n",
    "\n",
    "from mpl_toolkits.basemap import Basemap\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "#============================================# read data\n",
    "names = []\n",
    "pops  = []\n",
    "lats  = []\n",
    "lons  = []\n",
    "countries = []\n",
    "for line in file(\"../data/major_city\"):\n",
    "    info = line.split()\n",
    "    names.append(info[0])\n",
    "    pops.append(float(info[1]))\n",
    "    lat  = float(info[2][:-1])\n",
    "    if info[2][-1] == 'S': lat = -lat\n",
    "    lats.append(lat)\n",
    "    lon  = float(info[3][:-1])\n",
    "    if info[3][-1] == 'W': lon = -lon + 360.0\n",
    "    lons.append(lon)\n",
    "    country = info[4]\n",
    "    countries.append(country)\n",
    "\n",
    "#============================================\n",
    "# set up map projection with\n",
    "# use low resolution coastlines.\n",
    "map = Basemap(projection='ortho',lat_0=35,lon_0=120,resolution='l')\n",
    "# draw coastlines, country boundaries, fill continents.\n",
    "map.drawcoastlines(linewidth=0.25)\n",
    "map.drawcountries(linewidth=0.25)\n",
    "# draw the edge of the map projection region (the projection limb)\n",
    "map.drawmapboundary(fill_color='#689CD2')\n",
    "# draw lat/lon grid lines every 30 degrees.\n",
    "map.drawmeridians(np.arange(0,360,30))\n",
    "map.drawparallels(np.arange(-90,90,30))\n",
    "# Fill continent wit a different color\n",
    "map.fillcontinents(color='#BF9E30',lake_color='#689CD2',zorder=0)\n",
    "# compute native map projection coordinates of lat/lon grid.\n",
    "x, y = map(lons, lats)\n",
    "max_pop = max(pops)\n",
    "# Plot each city in a loop.\n",
    "# Set some parameters\n",
    "size_factor = 80.0\n",
    "y_offset    = 15.0\n",
    "rotation    = 30\n",
    "for i,j,k,name in zip(x,y,pops,names):\n",
    "    size = size_factor*k/max_pop\n",
    "    cs = map.scatter(i,j,s=size,marker='o',color='#FF5600')\n",
    "    plt.text(i,j+y_offset,name,rotation=rotation,fontsize=10)\n",
    "\n",
    "plt.title('Major Cities in Asia & Population')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "t = range(0,20)\n",
    "print t\n",
    "plt.plot(t,np.cos(t), color ='blue', linewidth=2.5, linestyle=\"--\")\n",
    "plt.scatter(t,np.cos(t), 50, color ='blue')\n",
    "plt.annotate('local max', xy=(2, 1), xytext=(3, 1.5),arrowprops=dict(facecolor='blue', shrink=0.03))\n",
    "\"\"\"\n",
    "plt.annotate(r'$\\sin(\\frac{2\\pi}{3})=\\frac{\\sqrt{3}}{2}$',\n",
    "             xy=(t, np.sin(t)), xycoords='data',\n",
    "             xytext=(+10, +30), textcoords='offset points', fontsize=16,\n",
    "             arrowprops=dict(arrowstyle=\"->\", connectionstyle=\"arc3,rad=.2\"))\n",
    "\n",
    "plt.plot([t,t],[0,np.sin(t)], color ='red', linewidth=2.5, linestyle=\"--\")\n",
    "plt.scatter([t,],[np.sin(t),], 50, color ='red')\n",
    "\n",
    "plt.annotate(r'$\\cos(\\frac{2\\pi}{3})=-\\frac{1}{2}$',\n",
    "             xy=(t, np.cos(t)), xycoords='data',\n",
    "             xytext=(-90, -50), textcoords='offset points', fontsize=16,\n",
    "             arrowprops=dict(arrowstyle=\"->\", connectionstyle=\"arc3,rad=.2\"))\n",
    "\"\"\"\n",
    "\n",
    "plt.grid()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'NoneType' object has no attribute 'tk'",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-27-1b84ecb2d3f8>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     42\u001b[0m ani = animation.FuncAnimation(fig, run, data_gen, blit=False, interval=10,\n\u001b[0;32m     43\u001b[0m                               repeat=False, init_func=init)\n\u001b[1;32m---> 44\u001b[1;33m \u001b[0mplt\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mshow\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32md:\\program files (x86)\\python\\python27\\lib\\site-packages\\matplotlib\\pyplot.pyc\u001b[0m in \u001b[0;36mshow\u001b[1;34m(*args, **kw)\u001b[0m\n\u001b[0;32m    242\u001b[0m     \"\"\"\n\u001b[0;32m    243\u001b[0m     \u001b[1;32mglobal\u001b[0m \u001b[0m_show\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 244\u001b[1;33m     \u001b[1;32mreturn\u001b[0m \u001b[0m_show\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkw\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    245\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    246\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32md:\\program files (x86)\\python\\python27\\lib\\site-packages\\matplotlib\\backend_bases.pyc\u001b[0m in \u001b[0;36m__call__\u001b[1;34m(self, block)\u001b[0m\n\u001b[0;32m    190\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    191\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mis_interactive\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mor\u001b[0m \u001b[0mget_backend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m'WebAgg'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 192\u001b[1;33m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmainloop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    193\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    194\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mmainloop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32md:\\program files (x86)\\python\\python27\\lib\\site-packages\\matplotlib\\backends\\backend_tkagg.pyc\u001b[0m in \u001b[0;36mmainloop\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m     72\u001b[0m \u001b[1;32mclass\u001b[0m \u001b[0mShow\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mShowBase\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     73\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mmainloop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 74\u001b[1;33m         \u001b[0mTk\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmainloop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     75\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     76\u001b[0m \u001b[0mshow\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mShow\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32md:\\program files (x86)\\python\\python27\\lib\\lib-tk\\Tkinter.pyc\u001b[0m in \u001b[0;36mmainloop\u001b[1;34m(n)\u001b[0m\n\u001b[0;32m    359\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mmainloop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mn\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    360\u001b[0m     \u001b[1;34m\"\"\"Run the main loop of Tcl.\"\"\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 361\u001b[1;33m     \u001b[0m_default_root\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtk\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmainloop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mn\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    362\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    363\u001b[0m \u001b[0mgetint\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mAttributeError\u001b[0m: 'NoneType' object has no attribute 'tk'"
     ],
     "output_type": "error"
    }
   ],
   "source": [
    "# 官方画图集锦：http://matplotlib.org/gallery.html，函数集锦：http://matplotlib.org/examples/index.html\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.animation as animation\n",
    "\n",
    "\n",
    "def data_gen(t=0):\n",
    "    cnt = 0\n",
    "    while cnt < 1000:\n",
    "        cnt += 1\n",
    "        t += 0.1\n",
    "        yield t, np.sin(2*np.pi*t) * np.exp(-t/10.)\n",
    "\n",
    "\n",
    "def init():\n",
    "    ax.set_ylim(-1.1, 1.1)\n",
    "    ax.set_xlim(0, 10)\n",
    "    del xdata[:]\n",
    "    del ydata[:]\n",
    "    line.set_data(xdata, ydata)\n",
    "    return line,\n",
    "\n",
    "fig, ax = plt.subplots()\n",
    "line, = ax.plot([], [], lw=2)\n",
    "ax.grid()\n",
    "xdata, ydata = [], []\n",
    "\n",
    "\n",
    "def run(data):\n",
    "    # update the data\n",
    "    t, y = data\n",
    "    xdata.append(t)\n",
    "    ydata.append(y)\n",
    "    xmin, xmax = ax.get_xlim()\n",
    "\n",
    "    if t >= xmax:\n",
    "        ax.set_xlim(xmin, 2*xmax)\n",
    "        ax.figure.canvas.draw()\n",
    "    line.set_data(xdata, ydata)\n",
    "\n",
    "    return line,\n",
    "\n",
    "ani = animation.FuncAnimation(fig, run, data_gen, blit=False, interval=10,\n",
    "                              repeat=False, init_func=init)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "Simple demo of the imshow function.\n",
    "\"\"\"\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.cbook as cbook\n",
    "\n",
    "image_file = cbook.get_sample_data('ada.png')\n",
    "image = plt.imread(image_file)\n",
    "\n",
    "plt.imshow(image)\n",
    "#plt.axis('off')  # clear x- and y-axes\n",
    "plt.plot(range(10),range(10))\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "Demo of image that's been clipped by a circular patch.\n",
    "\"\"\"\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.patches as patches\n",
    "import matplotlib.cbook as cbook\n",
    "\n",
    "\n",
    "image_file = cbook.get_sample_data('grace_hopper.png')\n",
    "image = plt.imread(image_file)\n",
    "\n",
    "fig, ax = plt.subplots()\n",
    "im = ax.imshow(image)\n",
    "patch = patches.Circle((260, 200), radius=200, transform=ax.transData)\n",
    "#im.set_clip_path(patch)\n",
    "\n",
    "plt.axis('off')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#!/usr/bin/env python\n",
    "\n",
    "from matplotlib.widgets import Cursor\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "\n",
    "fig = plt.figure(figsize=(8, 6))\n",
    "ax = fig.add_subplot(111, axisbg='#FFFFCC')\n",
    "\n",
    "x, y = 4*(np.random.rand(2, 100) - .5)\n",
    "ax.plot(x, y, 'o')\n",
    "ax.set_xlim(-2, 2)\n",
    "ax.set_ylim(-2, 2)\n",
    "\n",
    "# set useblit = True on gtkagg for enhanced performance\n",
    "cursor = Cursor(ax, useblit=True, color='red', linewidth=2)\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 中文显示，失败,参考：http://www.pythoner.com/200.html\n",
    "import matplotlib.pyplot as plt \n",
    "plt.xlabel(u'性别')\n",
    "plt.ylabel(u'人数')\n",
    "plt.xticks((0,1),(u'男',u'女'))\n",
    "plt.bar(left = (0,1),height = (1,0.5),width = 0.35,align=\"center\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\n"
     ]
    }
   ],
   "source": [
    "# pyhon GUI工具\n",
    "#import Tkinter as tk\n",
    "#mylabel = tk.Label(tk.Tk(),text='hello');\n",
    "from Tkinter import *\n",
    "mylabel = Label(Tk(),text='hello');\n",
    "mylabel.grid()\n",
    "print 'hello'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
